import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from '../views/Login.vue'
import Layout from '../views/layout'

Vue.use(VueRouter)

const routes = [
  {path:'/login',name:'login', component:Login},
  {
    path:'',
    component:Layout,
    redirect:'/users', //默认子路由
    children:[{
      path:'home',
      name:'home',
      component:()=>import ('../views/home')
    },{
      path:'users',
      name:'users',
      component:()=>import ('../views/users')
    },{
      path:'roles',
      name:'roles',
      component:()=>import ('../views/right/roles')
    },{
      path:'alloRights/:id',
      name:'alloRights',
      component:()=>import ('../views/right/roles/allocRight')
    },{
      path:'rights',
      name:'rights',
      component:()=>import ('../views/right/rights')
    },{
      path:'categories',
      name:'categories',
      component:()=>import('../views/commodity/categories')
    },{
      path:'params',
      name:'params',
      component:()=>import('../views/commodity/params')
    },{
      path:'goods',
      name:'goods',
      component:()=>import('../views/commodity/goods'),
    },{
      path:'goods/add',
      component:()=>import('../views/commodity/goods/addGoods')
    },{
      path:'orders',
      name:'orders',
      component:()=>import('../views/order')
    },{
      path:'reports',
      name:'reports',
      component:()=>import('../views/reports')
    }]
  }
]



const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})

router.beforeEach((to, from, next) => {
  const token = sessionStorage.getItem('token')
  if (to.name !== 'login' && !token) 
    next({ name: 'login' })
  else
    next()
})

export default router
